package com.example.backgroundtask;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;


public class ThreadActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = "ThreadActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread);
        findViewById(R.id.ac_thread_thread_btn).setOnClickListener(this);
        findViewById(R.id.ac_thread_runnable_btn).setOnClickListener(this);
        findViewById(R.id.ac_thread_callable_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ac_thread_thread_btn:
                goWorkThread();
                break;
            case R.id.ac_thread_runnable_btn:
                goWorkRunnable();
                break;
            case R.id.ac_thread_callable_btn:
                goWorkCallable();
                break;
        }
    }

    private void goWorkThread(){
        Log.i(TAG,"work1开始启动");
        WorkThread thread1 = new WorkThread("work1");
        thread1.start();
        Log.i(TAG,"work1启动完成");

        Log.i(TAG,"work2开始启动");
        WorkThread thread2 = new WorkThread("work2");
        thread2.start();
        Log.i(TAG,"work2启动完成");
    }

    private void goWorkRunnable(){
        Log.i(TAG,"Runnable1开始启动");
        WorkRunnable runnable1 = new WorkRunnable("Runnable1");
        Thread thread1 = new Thread(runnable1);
        thread1.start();
        Log.i(TAG,"Runnable1启动完成");

        Log.i(TAG,"Runnable2开始启动");
        WorkRunnable runnable2 = new WorkRunnable("Runnable2");
        Thread thread2 = new Thread(runnable2);
        thread2.start();
        Log.i(TAG,"Runnable2启动完成");
    }


    private void goWorkCallable()  {
        Log.i(TAG,"Callable开始启动");
        WorkCallable callable = new WorkCallable("callable1");
        FutureTask<Integer> task = new FutureTask<>(callable);
        Thread thread = new Thread(task);
        thread.start();
        try {
            Integer result = task.get();
            Log.i(TAG,"result ="+result);
        }catch (Exception e){
            Log.e(TAG,"Callable e="+e.toString());
        }
        Log.i(TAG,"Callable启动完成");

    }

    public class WorkThread extends Thread {
        private String name;
        public WorkThread(String name){
            this.name = name;
        }
        public void run() {
            Log.i(TAG,name+"-线程开始执行");
            for (int i=0;i<5;i++){
                Log.i(TAG,name+"执行 i="+i);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Log.i(TAG,name+"-线程执行结束");
        }
    }

    public class WorkRunnable implements Runnable{

        private String name;

        private int num = 0;
        public WorkRunnable(String name){
            this.name = name;
        }
        @Override
        public void run() {
            Log.i(TAG,name+"-线程开始执行");
            for (int i=0;i<5;i++){
//                Log.i(TAG,name+"执行 i="+i);
                num++;
                Log.i(TAG,name+"-num ="+num);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Log.i(TAG,name+"-线程执行结束");
        }
    }

    public class WorkCallable implements Callable<Integer> {
        private String name;

        public WorkCallable(String name){
            this.name = name;
        }

        public Integer call() throws Exception {
            Log.i(TAG,name+"-线程开始执行");
            int num = 0;
            for (int i=0;i<8;i++){
                Log.i(TAG,name+"-i ="+i);
                num++;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Log.i(TAG,name+"-线程执行结束");
            return num;
        }
    }


}